/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.administration;

import static controller.Connexion.CONF_DAO_INIT;
import dao.DAOInit;
import dao.interfaces.DAOCours;
import dao.interfaces.DAOEtudiant;
import gestionform.GestionAssoCours;
import gestionform.GestionCours;
import gestionform.GestionEtudiant;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Cour;
import model.Enseignant;
import model.Etudiant;
import model.Personne;

/**
 *
 * @author Paul
 */
public class AssociationCours extends HttpServlet {

    public static final String ACCES_CONNEXION = "/Connexion";
    public static final String VUE_ASSO_COURS = "/WEB-INF/administration/associationCours.jsp";
    public static final String VUE_ADMINISTRATION_ACCUEIL = "/WEB-INF/administration/administrationAccueil.jsp";
    public static final String ATT_GESTION_ASSO_COURS = "gAssoCours";
    public static final String ATT_GESTION_COURS = "gCours";
    public static final String ATT_GESTION_ETUDIANT = "gEtudiant";
    private DAOCours daoCours;
    private DAOEtudiant daoEtudiant;

    @Override
    public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Personne */
        this.daoCours = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOCours();
        this.daoEtudiant = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOEtudiant();

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enseignant ens = (Enseignant) request.getSession().getAttribute("enseignant");
        if (ens != null) {
            if (request.getSession().getAttribute("listeCours") != null
                    && request.getSession().getAttribute("listeEtudiants") != null
                    && request.getParameter("retourAdministration") != null) {
                this.getServletContext().getRequestDispatcher(VUE_ADMINISTRATION_ACCUEIL).forward(request, response);
            } else {
                HttpSession session = request.getSession(true);
                GestionCours gCours = new GestionCours(daoCours);
                ArrayList<Cour> listeCours = gCours.getListeCoursEnseignant(ens.getId());
                if (!gCours.getErreurs().isEmpty()) {
                    request.setAttribute(ATT_GESTION_COURS, gCours);
                } else {
                    session.setAttribute("listeCours", listeCours);
                    GestionEtudiant gEtudiant = new GestionEtudiant(daoEtudiant);
                    ArrayList<Etudiant> listeEtudiants = gEtudiant.getAllEtudiants();
                    if (gEtudiant.getErreurs().isEmpty()) {
                        session.setAttribute("listeEtudiants", listeEtudiants);
                    }
                    request.setAttribute(ATT_GESTION_ETUDIANT, gEtudiant);
                }
            }
            this.getServletContext().getRequestDispatcher(VUE_ASSO_COURS).forward(request, response);
        }
        else {
            request.getRequestDispatcher("view/connexion.jsp").forward(request, response);
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enseignant ens = (Enseignant) request.getSession().getAttribute("enseignant");
        if (ens != null) {
            GestionAssoCours gAssoCours = new GestionAssoCours();
            gAssoCours.checkSelectedValues(request);
            if (gAssoCours.getErreurs().isEmpty()) {
                GestionCours gCours = new GestionCours(daoCours);
                List<String> listeEtudiantsSelected = gAssoCours.getListeEtudiantsSelected();
                List<String> listeCoursSelected = gAssoCours.getListeCoursSelected();
                if (listeEtudiantsSelected.size() > 1 && listeCoursSelected.size() > 1) {
                    for (String coursSelected : listeCoursSelected) {
                        for (String etudiantSelected : listeEtudiantsSelected) {
                            gCours.attribuerCours(etudiantSelected, coursSelected);
                        }
                    }
                } else {
                    gCours.attribuerCours(listeEtudiantsSelected.get(0), listeCoursSelected.get(0));
                }
                request.setAttribute(ATT_GESTION_COURS, gCours);
            }
            request.setAttribute(ATT_GESTION_ASSO_COURS, gAssoCours);
            this.getServletContext().getRequestDispatcher(VUE_ASSO_COURS).forward(request, response);
        } else {
            request.getRequestDispatcher("view/connexion.jsp").forward(request, response);
        }
    }
}
